package com.xz.group12306.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xz.group12306.business.service.UserService;
import com.xz.group12306.business.service.Impl.UserServiceImpl;
import com.xz.group12306.domain.User;

/**
 * Servlet implementation class UpdateMassageServlet
 */
@WebServlet("/UpdateMassageServlet")
public class UpdateMassageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateMassageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		String id=request.getParameter("id");
		String username=request.getParameter("username");
		String content=request.getParameter("content");
		System.out.println(id+"\t"+username+"\t"+content);
		User user3=(User) request.getSession().getAttribute("user");
		User user=new User();
		user.setId(Integer.parseInt(id));
		user.setUsername(username);
		user.setRealname(user3.getRealname());
		user.setSex(user3.getSex());
		user.setBirthday(user3.getBirthday());
		user.setContent(content);
		UserService userService=new UserServiceImpl();
		userService.updateUser(user);
		HttpSession session=request.getSession();
		
		session.removeAttribute("user"); 
		
		session.setAttribute("user", user);
		response.getWriter().print("<script>window.location.href='user/index.jsp';</script>");
	}

}
